package com.shop.exception;

import com.shop.model.vo.Rsp;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.UnexpectedTypeException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author : WanS
 * @date : 10:21 2020/12/1
 */
@RestControllerAdvice
public class GlobalException {
    /*
     * @author Clh
     * @description 捕获后台数据异常
     * @date  2020/11/2 17:32
     * @param e 异常
     * @return cn.miracle.response.ServiceResponse
     **/
    @ExceptionHandler({MethodArgumentNotValidException.class,BindException.class, ConstraintViolationException.class})
    public Rsp paramCheck(Exception e){
        if (e instanceof MethodArgumentNotValidException){
            MethodArgumentNotValidException m= (MethodArgumentNotValidException) e;
            BindingResult result = m.getBindingResult();
            for (FieldError fieldError:result.getFieldErrors()) {
                return Rsp.fail();
            }
        }else if (e instanceof BindException){
            BindException b= (BindException) e;
            for (FieldError fieldError:b.getFieldErrors()){
                return Rsp.fail();
            }
        }else {
            ConstraintViolationException  c = (ConstraintViolationException) e;
            Set<ConstraintViolation<?>> set=c.getConstraintViolations();
            for (ConstraintViolation<?> constraintViolation:
                    set) {
                return Rsp.fail();
            }
        }
        return null;
    }
    /**
     * @author Clh
     * @date  2020/11/2 17:25
     * @param e 截取下标异常
     * @return cn.miracle.response.ServiceResponse
     **/
    @ExceptionHandler(java.lang.StringIndexOutOfBoundsException.class)
    public Rsp StringIndexOutOfBoundsException(StringIndexOutOfBoundsException e){
//        e.printStackTrace();
        return Rsp.fail();
    }

    /**
     * @author Clh
     * @date  2020/11/2 17:25
     * @param e 参数非法异常
     * @return cn.miracle.response.ServiceResponse
     **/
    @ExceptionHandler(IllegalStateException.class)
    public Rsp IllegalStateException(IllegalStateException e){
//        e.printStackTrace();
        return Rsp.fail();
    }

    @ExceptionHandler(IOException.class)
    public Rsp ClaimJwtException(IOException e){
//        e.printStackTrace();
        return Rsp.fail();
    }
    /**
     * @author yyt
     * @date
     * @param e token异常处理
     * @return
     **/
//    @ExceptionHandler({RuntimeException.class})
//    public Rsp verifyTokenException(RuntimeException e, BindingResult bindingResult){
//        Map<String,Object> map = new HashMap<>();
//        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
//        fieldErrors.forEach(error ->
//                map.put(error.getField(),error.getDefaultMessage()));
//        return Rsp.fail();
//    }
    /**
     * @author Clh
     * @description
     * @date  2020/11/4 16:34
     * @param e 捕获数据格式异常
     * @return cn.miracle.response.ServiceResponse
     **/
    @ExceptionHandler({HttpMessageNotReadableException.class, UnexpectedTypeException.class})
    public Rsp HttpMessageNotReadableException(HttpMessageNotReadableException e) {
//        e.printStackTrace();
        return Rsp.fail();
    }

    /**
     * 捕获空指针异常
     * @return
     */
    @ExceptionHandler(NullPointerException.class)
    public Map<String,Object> nullException(){
        Map<String,Object> map = new HashMap<>();
        map.put("status",1);
        map.put("msg","出现空指针异常");
        return map;
    }


    @ExceptionHandler(RuntimeException.class)
    public Rsp verifyTokenException(RuntimeException e){
        return Rsp.fail().msg("运行时异常");
    }

}
